package com.example.blades.diary;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.litepal.LitePal;

import java.text.DateFormat;
import java.util.Date;


public class DiaryEditor extends AppCompatActivity {
    private long id;
    private long idx;
    private EditText edtitle;
    private EditText edbody;
    private String date;
    Button btn_confirm=null;
    Button btn_cancel=null;
    final String EDIT="EDIT";
    final String NEW="NEW";
    private static final int STATE_INSERT = 1;
    private int state;
    private String action;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diary_editor);

        btn_cancel=(Button)findViewById(R.id.cancel);
        btn_confirm=(Button)findViewById(R.id.confirm);
        edtitle=(EditText)findViewById(R.id.title);
        edbody=(EditText)findViewById(R.id.body);

        /*设置EditText的显示方式为多行文本输入*/
        edbody.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
        /*文本显示的位置在EditText的最上方*/
        edbody.setGravity(Gravity.TOP);
        /*改变默认的单行模式*/
        edbody.setSingleLine(false);
        /*水平滚动设置为False*/
        edbody.setHorizontallyScrolling(false);

        Bundle bundle = new Bundle();
        bundle = this.getIntent().getExtras();
        action=bundle.getString("action");

        if(EDIT.equals(action))
        {
            idx = bundle.getLong("id");
            Log.d("action", action);
            Note note = LitePal.find(Note.class, idx);
            Log.v("Tag", String.valueOf(idx));
            Log.v("Tag title", note.getTitle());
            edtitle.setTextKeepState(note.getTitle());
            edbody.setTextKeepState(note.getBody());
            setTitle("编辑日记");
        }
        else if(NEW.equals(action))
        {
            state=STATE_INSERT;
            setTitle("新建日记");
        }
        else
        {
            Log.e("Tag", "no such action,Error");
            finish();
            return;
        }

        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {

                if(state==STATE_INSERT) {
                    insertData();
                }
                else
                {
                    updateData();
                }
                Intent intent = new Intent(DiaryEditor.this, MainActivity.class);
                startActivity(intent);
                DiaryEditor.this.finish();
            }
        });

        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DiaryEditor.this, MainActivity.class);
                startActivity(intent);
                DiaryEditor.this.finish();
            }
        });



    }

    /*新建日记并插入*/
    public void insertData()
    {
        String title=edtitle.getText().toString();
        String body=edbody.getText().toString();

        final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
                DateFormat.MEDIUM);
        String date = df.format(new Date());

        Note note=new Note();
        note.setTitle(title);
        note.setBody(body);
        note.setDate(date);
        note.save();
        if (note.save()) {
            Toast.makeText(this, "日记保存成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "日记保存失败", Toast.LENGTH_SHORT).show();
        }
        Log.d("DaoExample", "Inserted new note, ID: " + note.getId());
    }

    public void updateData()
    {
        String title=edtitle.getText().toString();
        String body=edbody.getText().toString();
        final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
                DateFormat.MEDIUM);
        String date = df.format(new Date());

        Note note=new Note();
        note.setTitle(title);
        note.setBody(body);
        note.setDate(date);
        note.save();
        note.save();
        if (note.save()) {
            Toast.makeText(this, "日记保存成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "日记保存失败", Toast.LENGTH_SHORT).show();
        }
        Log.d("DaoExample", "Inserted new note, ID: " + note.getId());
    }


}
